﻿Public Class Stock
    Public l As New Label
    Public pro As Integer = 0
    Public store As Integer = 0
    Private Sub Stock_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadproduct()
        loadstore()
        If pro Or store <> 0 Then
            cbopro.SelectedValue = pro
            cbostore.SelectedValue = store
        End If
        
    End Sub
    Private Sub loadproduct()
        cbopro.ValueMember = ("ProductId")
        cbopro.DisplayMember = ("ProductName")
        cbopro.DataSource = cmd.ExecuteDataTable("SELECT ProductId,ProductName FROM Product WHERE Active=1")
    End Sub
    Private Sub loadstore()
        cbostore.ValueMember = ("StoreId")
        cbostore.DisplayMember = ("StoreName")
        cbostore.DataSource = cmd.ExecuteDataTable("SELECT StoreId,StoreName FROM Store WHERE Active=1")

    End Sub

    Private Sub btnok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnok.Click
        If txtqty.Text.Trim = "" Then
            MessageBox.Show("Qunatity Can't be Empty")
            txtqty.Focus()
            Exit Sub
        End If
        If Validater.IsDecimal(txtqty.Text) = False Then
            MessageBox.Show("Quantity must be Decimal Number")
            txtqty.SelectAll()
            Exit Sub
        End If

        cmd.Parameters("@pid") = cbopro.SelectedValue
        cmd.Parameters("@sid") = cbostore.SelectedValue
        cmd.Parameters("@qty") = txtqty.Text
        If Me.l.Text = "" Then
            cmd.ExecuteNonQuery("INSERT INTO Stock VALUES(@pid,@sid,@qty,1);")
        Else
            cmd.Parameters("@stId") = l.Text
            cmd.ExecuteNonQuery("UPDATE Stock SET ProductId=@pid,StoreId=@sid,Qty=@qty WHERE StockId=@stId")
        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub
End Class